#include "dashboard_widget2.h"

#include <QPropertyAnimation>
#include <QPainter>

#include <QDebug>

struct DashBoardWidget2Private{
    //旋转角度
    //缓解位：70；运转位：40；保压位：10；制动位：-25
    int rotation = -25;

    //旋转角度的最大值和最小值
    int maxValue = 70;
    int minValue = -25;

    QColor backgroundColor = Qt::transparent;

    //仪表指针1：属性动画
    QPropertyAnimation *animation;

};

DashBoardWidget2::DashBoardWidget2(QWidget *parent)
    : QWidget(parent)
    , d(new DashBoardWidget2Private)
{

    d->animation = new QPropertyAnimation(this, "rotation", this);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    //手柄旋转动画
    connect(this, &DashBoardWidget2::valueChanged,
            this, &DashBoardWidget2::onStartAnimation);

    //手柄下压动画
    connect(this, &DashBoardWidget2::gatePressDownStatusChanged,
            this, [&](bool gatePressDownStatus){
                m_gatePressDownStatus = gatePressDownStatus;

                onStartAnimation(d->rotation);
            });
}

DashBoardWidget2::~DashBoardWidget2()
{

}

//绘制物体
void DashBoardWidget2::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //背景
    if (d->backgroundColor != Qt::transparent) {
        painter.setPen(Qt::NoPen);
        painter.fillRect(rect(), d->backgroundColor);
    }

    // 指定要绘制的图片（将图片路径替换为有效的图片路径）
    //绘制仪表图像
    QRect rect;
    rect.setRect(100, 0, 200, 200);
    painter.setRenderHint(QPainter::Antialiasing, true); // 启用抗锯齿(反走样)
    painter.drawPixmap(rect, QPixmap(m_meterImagePath));

    //绘制手柄
    drawPole(&painter);
}

void DashBoardWidget2::drawPole(QPainter *painter)
{
    painter->save();

    //更改旋转坐标中心点的坐标（200,100）
    painter->translate(200, 100);

    //初始旋转角度
    // painter->rotate(-25);
    double degRotate = d->rotation;

    painter->rotate(degRotate);

    QRect rect;
    // rect.setRect(0, 100, 30, 110);
    if(m_gatePressDownStatus == false){
        rect.setRect(0, 100, 30, 110);
    }else{
        rect.setRect(0, 100, 20, 90);
    }
    painter->drawPixmap(rect, QPixmap(":/Image/pole.png"));

    painter->restore();
}

void DashBoardWidget2::onStartAnimation(const int value)
{
    //qDebug()<<"d->rotation:"<<d->rotation;
    //qDebug()<<"value:"<<value;

    int start = d->rotation;
    int end = value;

    // 设置动画起始值和结束值
    d->animation->setStartValue(start);
    d->animation->setEndValue(end);
    // 设置动画曲线，这里使用了缓慢进入、缓慢退出的曲线效果
    d->animation->setEasingCurve(QEasingCurve::InOutQuad);
    // 启动动画
    d->animation->start();
}

int DashBoardWidget2::rotation() const
{
    return d->rotation;
}

void DashBoardWidget2::setRotation(const int rotation)
{
    d->rotation = rotation;
    update();
}

void DashBoardWidget2::setMin(const double min)
{
    d->minValue = min;
    update();
}

double DashBoardWidget2::min() const
{
    return d->minValue;
}

void DashBoardWidget2::setmax(const double max)
{
    d->maxValue = max;
    update();
}

double DashBoardWidget2::max() const
{
    return d->maxValue;
}

void DashBoardWidget2::setMeterImagePath(QString imagePath)
{
    m_meterImagePath = imagePath;
}
